package com.jt.flight.controller;

import com.jt.flight.domain.Av;
import com.jt.flight.domain.CityRelation;
import com.jt.flight.model.Response;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.mvc.annotation.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * since 2015/10/29.
 */
@Controller
@RequestMapping("/av")
public class AvController extends BaseController {

    @RequestMapping("/add")
    @ResponseBody
    public Object add(@RequestBody Av av) {
        CityRelation cr = dao.fetch(CityRelation.class, Cnd.where("fromCode", "=", av.getDepAirport()).and("toCode", "=", av.getArrAirport()));
        if (cr == null) {
            cr = new CityRelation();
            cr.setFromCode(av.getDepAirport());
            cr.setToCode(av.getArrAirport());
            dao.insert(cr);
        }
        av.setCityRelationId(cr.getId());
        Av insert = dao.insert(av);
        if (insert.getId() > 0) {
            return Response.success("", "");
        } else {
            return Response.fail("insert error");
        }
    }

    @RequestMapping
    public String index() {
        return "av/list";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(Pager pager) {
        QueryResult result = new QueryResult();
        List<Av> query = dao.query(Av.class, null, pager);
        int count = dao.count(Av.class);
        pager.setRecordCount(count);
        result.setList(query);
        result.setPager(pager);
        return result;
    }


}
